<?php

class Theme extends ecsTemplate
{
    var $_currentNode    = null;
    var $_currentLibrary = null;
    var $config          = null;
    var $doc_type        = '';
    var $store_id        = 0;
    var $edit_mode       = false;

    function __construct()
    {
        $this->Theme();
    }

    function Theme()
    {
        parent::__construct();
    }

    function check_edit_mode()
    {
        if (isset($_SESSION['admin_id']) && $_SESSION['admin_id'] > 0 && isset($_GET['edit_mode']))
        {
            require_once(ROOT_PATH . '/includes/models/mod.adminuser.php');

            $admin = new AdminUser($_SESSION['admin_id'], $_SESSION['store_id']);
            if (!$admin->check_priv('template', 'edit'))
            {
                return false;
            }

            $mall_allowed  = array('homepage', 'goods_list');
            $store_allowed = array('homepage', 'goods', 'goods_list');

            if ($_SESSION['store_id'] > 0 && $_SESSION['store_id'] == $this->store_id)
            {
                if (basename($this->template_dir) == 'store' && in_array($this->pagename, $store_allowed))
                {
                    return true;
                }
            }

            else if(!empty($_SESSION['admin_id']) && empty($_SESSION['store_id']))
            {
                if (basename($this->template_dir) == 'mall' && in_array($this->pagename, $mall_allowed))
                {
                    return true;
                }
            }
        }
        return false;
    }

    function display($pagename, $cache_id = '')
    {
    	
        $this->pagename = $pagename;
        $editable = false;
        $options = $this->options;
        $cache_array = array(
                        'MemberController',
                        'Test_home_adController',
                        //'StoreindexController',
                        'Test_store_adController',
                        //'OutsideInviteController',
                        //'StoreListController',
                        );
        if(in_array($options['controller'],$cache_array))
        {
            $this->_expires = time() + 86400;
        }

        if ($this->check_edit_mode())
        {
        	
            $this->direct_output = true;
            $this->force_compile = true;
            $this->caching = false;
            $editable = true;

            $lang = Language::load_lang(lang_file("admin/template"));
            $this->assign('lang', $lang);

            $this->assign('editing', true);
            $this->assign('pagename', $this->pagename);

            $template_data_file = ROOT_PATH . '/temp/tdata/'. $this->pagename . '_' . intval($_SESSION['store_id']) .'.php';

            if (!is_dir(ROOT_PATH . '/temp/tdata/'))
            {
                ecm_mkdir('/temp/tdata');
            }
            $str = "<?php\r\n";
            $str .= '$tempate_data = ' . var_export($this->_var , true);
            $str .= "\r\n?>";
            file_put_contents($template_data_file, $str, LOCK_EX);
            $this->edit_mode = true;
        }

        #### 静态化统一做，不用template组件的静态化页面生成功能 modified by caiyizhen ####
        if (!empty($this->store_id))
        {
            $this->compile_sub_dir .= $this->compile_dir . '/' . md5($pagename . $this->_page->filename . $this->skin);
        }
  
        $this->assign('store_id', $this->store_id);

        $out = $this->fetch($pagename, $cache_id);

        if (strpos($out, $this->_echash) !== false)
        {
            $k = explode($this->_echash, $out);
            foreach ($k AS $key => $val)
            {
                if (($key % 2) == 1)
                {
                    $k[$key] = $this->insert_mod($val);
                }
            }
            $out = implode('', $k);
        }

        error_reporting($this->_errorlevel);
        $this->_seterror--;

        if ($this->gzip)
        {
            ob_start('ob_gzhandler');
        }
        else
        {
            ob_start();
        }

        echo $out;
    }

    function _get_code()
    {
        #### 静态化统一做，不用template组件的静态化页面生成功能 modified by caiyizhen ####
        include_once(ROOT_PATH . '/includes/models/mod.page.php');
   
        $this->_page = new Page($this->pagename, $this->store_id, $editable);
        $this->_page->current_skin = $this->skin;
        $this->_page->mall_skin    = $this->options['mall_skin'];
        $this->_page->controller_name = $this->options['controller'];
        $this->_current_file   = $this->_page->filename;

        return $this->_page->get_html();
    }

    function fetch($pagename, $cache_id = '')
    {
        if(strpos($pagename, "resource/") !== false || $pagename == '')
        {
            return parent::fetch($pagename);
        }

        if (!$this->_seterror)
        {
            error_reporting(E_ALL ^ E_NOTICE);
        }

        $this->_seterror++;

        if (strncmp($pagename,'str:', 4) == 0)
        {
            $out = parent::_eval(parent::fetch_str(substr($pagename, 4)));
        }
        else
        {
            if ($this->direct_output)
            {

                $code = $this->_get_code();
                $out = parent::_eval(parent::fetch_str($code));
            }
            else
            {
                $out = $this->make_compiled($pagename);
                #### 静态化统一做，不用template组件的静态化页面生成功能 modified by caiyizhen ####
            }
        }

        $this->_seterror--;
        if (!$this->_seterror)
        {
            error_reporting($this->_errorlevel);
        }

        return $out;
    }

    function product_dir($dir)
    {
        $array_dir=explode('/',$dir);//把多级目录分别放到数组中
        for($i=0;$i<count($array_dir);$i++){
           $path .= $array_dir[$i].'/';
           if(!file_exists($path)){
                mkdir($path);
           }
        }
    }

    function make_compiled($pagename)
    {
        $this->pagename = $pagename;
        if(strpos($pagename, "resource/")!==false)
        {
            return parent::make_compiled($pagename);
        }

        $name  = isset($this->compile_sub_dir) ? $this->compile_sub_dir : $this->compile_dir;
        $name .= '/' . $pagename . '.layout.php';
        $exists = is_file($name);

        if ($this->_expires)
        {
            $expires = $this->_expires - $this->cache_lifetime;
        }
        else
        {
            if ($exists)
            {
                $filestat = @stat($name);
                $expires  = $filestat['mtime'];
            }
            else
            {
                $expires = 0;
            }
        }

        #### 静态化统一做，不用template组件的静态化页面生成功能 modified by caiyizhen ####
        if ($exists)
            $source = $this->_require($name);
        else
        {
            $content = $this->_get_code();

            $source = $this->fetch_str($content);

            if (!empty($this->compile_sub_dir))
            {
                $dirname = str_replace(ROOT_PATH, '', $this->compile_sub_dir);
                $name = $this->compile_sub_dir . "/" . basename($name);
                ecm_mkdir($dirname);
            }
            if (file_put_contents($name, $source, LOCK_EX) === false)
            {
                trigger_error('can\'t write:' . $name);
            }

            $source = $this->_eval($source);
        }

        return $source;
    }

}

?>
